package com.dsbj.mp.icardservice.controller.physicalcondition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.entity.DayPhysicalCondition;
import com.dsbj.mp.icardservice.entity.Employee;
import com.dsbj.mp.icardservice.entity.EmployeeWelfare;
import com.dsbj.mp.icardservice.entity.Persont;
import com.dsbj.mp.icardservice.model.MpUser;
import com.dsbj.mp.icardservice.service.EmployeeService;
import com.dsbj.mp.icardservice.service.EmployeeWelfareService;
import com.dsbj.mp.icardservice.service.PersontService;
import com.dsbj.mp.icardservice.service.physicalcondition.DayPhysicalConditionService;
import com.dsbj.mp.icardservice.utils.strprocess.StrUtil;
import com.dsbj.mp.icardservice.utils.strprocess.Time_convert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * dayPhysicalConditon服务类。
 *
 * @author cjw 2020-02-15
 */
@Service
@Transactional
public class UcDayPhysicalCondition {
	
	@Resource
	private DayPhysicalConditionService dayPhysicalConditionService;
	
	@Resource
	private EmployeeService employeeService;
	
	@Resource
	private EmployeeWelfareService employeeWelfareService;
	
	@Resource
	private PersontService persontService;
	
	
	/**
	 * 判断checkTime对应的这一天是否可以提交健康状况信息。
	 * 该方法的流程为:
	 * 1,若该日期未到来,不可提交;
	 * 2,若该日期已到来, 若已提交过,不可再提交; 若没提交过,可提交。
	 * <p>
	 * 传入参数: checkTime
	 *
	 * @return
	 * @params
	 * @author cjw 2020/2/10
	 */
	public ResponseMessage canSubmitByCheckTime(MpUser wxUser, String checkTime) {
		Map<String, Object> mapReturn = new LinkedHashMap<>();
		mapReturn.put("canSubmit", 1);
		mapReturn.put("message", "可以提交健康状况信息");
		
		//若该日期未到来
		long timeDiff = Time_convert.convert_todate(checkTime).getTime() - System.currentTimeMillis();
		if (timeDiff > 0) {
			mapReturn.put("canSubmit", 0);
			mapReturn.put("message", "不可提交今后的健康状况信息");
			return ResponseMessage.ok(mapReturn);
		}
		
		//若该日期已到来,分析是否提交过
		QueryWrapper<DayPhysicalCondition> queryWrapper = new QueryWrapper<DayPhysicalCondition>()
				.eq("IDCARDNO", wxUser.getIdcardno())
				.eq("TO_CHAR(CHECK_TIME,'yyyy-mm-dd')", checkTime)
				.eq("STATUS", 1);
		List<DayPhysicalCondition> list_dayPhysicalCondition = dayPhysicalConditionService.list(queryWrapper);
		//若已提交过
		if (!list_dayPhysicalCondition.isEmpty()) {
			mapReturn.put("canSubmit", 0);
			mapReturn.put("message", "这一天您已提交过健康状况信息");
			return ResponseMessage.ok(mapReturn);
		}
		//若未提交过
		return ResponseMessage.ok(mapReturn);
	}
	
	/**
	 * 用"最近的完整的"健康状况信息,补充该dayPhysicalCondition,并返回该对象。
	 * 传入的dayPhysicalCondition为"今天没有更新"的对象。
	 *
	 * @param dayPhysicalCondition
	 * @return
	 */
	public ResponseMessage supplementDayPhysicalCondition(DayPhysicalCondition dayPhysicalCondition) {
		//验证dayPhysicalCondition
		if (dayPhysicalCondition == null) {
			return ResponseMessage.error("传入的健康状况为null");
		}
		if (dayPhysicalCondition.getUpdateToday() == null) {
			return ResponseMessage.error("udpateToday为null");
		}
		if (dayPhysicalCondition.getUpdateToday() == 1) {
			return ResponseMessage.error("updateToday为1,不需要补充完整!");
		}
		
		/**
		 * dayPhysicalCondition.updateToday为0时,用"最近的完整的"健康状况信息,补充该dayPhysicalCondition。
		 */
		//获取"最近的完整的"dayPhysicalCondition信息
		QueryWrapper<DayPhysicalCondition> queryWrapper = new QueryWrapper<DayPhysicalCondition>()
				.eq("IDCARDNO", dayPhysicalCondition.getIdcardno())
				.eq("UPDATE_TODAY", 1)
				.eq("STATUS", 1)
				.lt("CHECK_TIME", dayPhysicalCondition.getCheckTime())
				.orderByDesc("CHECK_TIME");
		List<DayPhysicalCondition> list_dayPhysicalCondition = dayPhysicalConditionService.list(queryWrapper);
		if (list_dayPhysicalCondition.isEmpty()) {
			return ResponseMessage.error("用\"最近的完整的\"健康状况信息,补充该dayPhysicalCondition失败,未找到\"最近的完整的\"健康状况信息!");
		}
		
		//用"最近的完整的"健康状况信息,补充该dayPhysicalCondition
		DayPhysicalCondition dayPhysicalCondition_pre = list_dayPhysicalCondition.get(0);
		dayPhysicalCondition_pre.setId(null);
		
		//昨天下班或休息日是否外出
		dayPhysicalCondition_pre.setOutYesterday(dayPhysicalCondition.getOutYesterday());
		dayPhysicalCondition_pre.setOutLocation(dayPhysicalCondition.getOutLocation());
		dayPhysicalCondition_pre.setOutLocationDes(dayPhysicalCondition.getOutLocationDes());
		dayPhysicalCondition_pre.setPersonOutTogether(dayPhysicalCondition.getPersonOutTogether());
		dayPhysicalCondition_pre.setOutTransport(dayPhysicalCondition.getOutTransport());
		dayPhysicalCondition_pre.setOutTransportDes(dayPhysicalCondition.getOutTransportDes());
		dayPhysicalCondition_pre.setPersonalProtect(dayPhysicalCondition.getPersonalProtect());
		//打卡日期
		dayPhysicalCondition_pre.setCheckTime(dayPhysicalCondition.getCheckTime());
		dayPhysicalCondition_pre.setTemperature(dayPhysicalCondition.getTemperature());
		dayPhysicalCondition_pre.setUpdateToday(dayPhysicalCondition.getUpdateToday());
		dayPhysicalCondition_pre.setCreateTime(new Date());
		dayPhysicalCondition_pre.setUpdateTime(null);
		dayPhysicalCondition_pre.setUpdateUser(null);
		
		return ResponseMessage.ok(dayPhysicalCondition_pre);
	}
	
	/**
	 * 获取员工手机号码。
	 * @param
	 * @return
	 * @author cjw 2020-02-19
	 */
	public ResponseMessage getEmplPhoneNumByIdcardno(MpUser mpUser) {
		//返回结构体
		Map<String, Object> mapReturn = new LinkedHashMap<>();
		mapReturn.put("phoneNum", null);
		
		/**
		 * 从employee,persont表中获取该员工信息。先查询employee表,若未找到该员工信息,再查询persont表。
		 */
		QueryWrapper<Employee> queryWrapper_employee = new QueryWrapper<Employee>()
				.eq("IDCARDNO", mpUser.getIdcardno())
				.isNull("DISMISSIONDATE")
				.eq("DELETESTATUS", 1);
		Employee employee = employeeService.getOne(queryWrapper_employee);
		//若employee不为null
		if (employee != null) {
			//从员工附加信息表获取该员工的手机号
			QueryWrapper<EmployeeWelfare> queryWrapper_employeeWelfare = new QueryWrapper<EmployeeWelfare>()
					.eq("EMPLOYEENO", employee.getEmployeeno());
			EmployeeWelfare employeeWelfare = employeeWelfareService.getOne(queryWrapper_employeeWelfare);
			if (employeeWelfare == null) {
				return ResponseMessage.error("未找到员工附加信息记录");
			}
			if (StrUtil.isEmpty(employeeWelfare.getContactway())) {
				return ResponseMessage.error("员工手机号码不存在");
			}
			mapReturn.put("phoneNum", employeeWelfare.getContactway());
			return ResponseMessage.ok(mapReturn);
		}
		
		//若employee为null,从persont表中获取员工信息
		if (employee == null) {
			QueryWrapper<Persont> queryWrapper_persont = new QueryWrapper<Persont>()
					.eq("IDCARDNO", mpUser.getIdcardno())
					.eq("STATUS", 1);
			Persont persont = persontService.getOne(queryWrapper_persont);
			if (persont == null) {
				return ResponseMessage.error("未找到该员工信息");
			}
			if (StrUtil.isEmpty(persont.getPhoneNumber())) {
				return ResponseMessage.error("员工手机号不存在");
			}
			mapReturn.put("phoneNum", persont.getPhoneNumber());
			return ResponseMessage.ok(mapReturn);
		}
		
		return ResponseMessage.ok(mapReturn);
	}
	
	
}
